package com.ucomm.talent.activity;

import java.util.ArrayList;
import java.util.Date;

import me.maxwin.view.IXListViewLoadMore;
import me.maxwin.view.IXListViewRefreshListener;
import me.maxwin.view.XListView;

import com.ucomm.talent.AppConfig;
import com.ucomm.talent.AppConstants;
import com.ucomm.talent.AppException;
import com.ucomm.talent.R;
import com.ucomm.talent.activity.detail.JobFairDetailsActivity;
import com.ucomm.talent.adapter.JobFairInfoListAdapter;
import com.ucomm.talent.common.DebugUtil;
import com.ucomm.talent.manager.SearchManager;
import com.ucomm.talent.model.JobFairInfo;
import com.ucomm.talent.widget.CustomProgressDialog;
import com.ucomm.talent.widget.MainHeaderBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
/**
 * 招聘现场
 */

public class JobFairActivity extends Activity implements IHeaderBarClickListener{
	
	private MainHeaderBar mainHeaderBar;
	private XListView listView;
	private CustomProgressDialog customProgressDialog;
	private SearchManager searchManager;
	private JobFairInfoListAdapter adapter;
	private ArrayList<JobFairInfo> data;
	private String url = null;
	
	Handler mHandler = new Handler(){
	        @Override
	        public void handleMessage(Message msg) {
	            int what = msg.what;
	            DebugUtil.Log(JobFairActivity.this,"---------- " + customProgressDialog.toString());
	            switch (what){
	                case AppConstants.MsgStatus.MSG_ERROR:
	                    if (customProgressDialog != null && customProgressDialog.isShowing())
	                        customProgressDialog.dismiss();
	                    ((AppException)msg.obj).makeToast(JobFairActivity.this);
	                    break;
	                case AppConstants.MsgStatus.MSG_SUCCUSS:
	                    if (customProgressDialog != null && customProgressDialog.isShowing())
	                        customProgressDialog.dismiss();
	                    //放置数据
	                    data.addAll((ArrayList<JobFairInfo>) msg.obj);
	                    if(adapter==null){
		                    adapter = new JobFairInfoListAdapter(JobFairActivity.this, data);
		                    listView.setAdapter(adapter);
	                    }else{
	                    	adapter.notifyDataSetChanged();
	                    }
	                    listView.setOnItemClickListener(new ListItemListener());
	                  //上拉加载更多
	                    listView.setPullLoadEnable(new IXListViewLoadMore() {
	                        @Override
	                        public void onLoadMore() {
	                        	if(AppConfig.CURR_PAGE !=0){
	                        		int i = AppConfig.CURR_PAGE;
	                        		if(i<AppConfig.PAGE_COUNT){
	                        			AppConfig.CURR_PAGE += 1;
	                        			String pageUrl = url+"&page="+AppConfig.CURR_PAGE;
	                                	searchManager.searchDataFromNetwrok(pageUrl);
	                        		}else{
	                        			listView.disablePullLoad();
	                        		}
	                        	}
	                        }
	                    });
	                    //下拉刷新
	                    listView.setPullRefreshEnable(new IXListViewRefreshListener() {
	                        @Override
	                        public void onRefresh() {
	                        	searchManager.searchDataFromNetwrok(url);
	                        	listView.stopRefresh(AppConfig.formatter.format(new Date(System.currentTimeMillis())));
	                        }
	                    });
	                    break;
	                case AppConstants.MsgStatus.MSG_LOADING:
	                    if (customProgressDialog != null && !customProgressDialog.isShowing())
	                        customProgressDialog.show();
	                    break;
	                default:
	                    break;
	            }
	            super.handleMessage(msg);
	        }
	    };
	    protected void onCreate(Bundle savedInstanceState) {
		    super.onCreate(savedInstanceState);
		    setContentView(R.layout.job_fair_layout);
		    initView();
		    searchManager = new SearchManager(this, mHandler);
		    Intent i = getIntent();
		    String otherUrl = i.getStringExtra("url");
		    if (otherUrl!=null){
		    	url = otherUrl;
		    }else{
		    	url = AppConstants.JOB_FAIR;
		    }
		    searchManager.searchDataFromNetwrok(url);
	    }
	    
	    private void initView(){
	        mainHeaderBar = (MainHeaderBar) findViewById(R.id.main_header_bar);
	        mainHeaderBar.setBarClickListener(this);
	        customProgressDialog = new CustomProgressDialog(this);
	        listView = (XListView) findViewById(R.id.custom_list);
	        listView.setmHeaderViewBackground(R.color.custom_list_item_bg_color);
	        listView.setmFooterViewBackground(R.color.custom_list_item_bg_color);
	        data = new ArrayList<JobFairInfo>();
	
	    }
	    
    class ListItemListener implements OnItemClickListener{
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			ArrayList<JobFairInfo> mData = adapter.getmData();
			JobFairInfo info = mData.get(arg2-1);
			Intent i = new Intent();
			i.putExtra("info", info);
			i.setClass(JobFairActivity.this, JobFairDetailsActivity.class);
			startActivity(i);
		}
	}
	@Override
	public void onBackButtOnClick(View view) {
		finish();
	}

	@Override
	public void onExitButtonOnClick(View view) {
		// TODO Auto-generated method stub
		
	}
	
}
